<template>
  <div
    v-infinite-scroll="handleInfiniteOnLoad"
    class="demo-infinite-container"
    :infinite-scroll-disabled="busy"
    :infinite-scroll-distance="10"
  >
    <a-config-provider>
      <template v-if="data.length == 0" #renderEmpty>
        <div style="text-align: center">
          <a-icon type="smile" style="font-size: 20px; margin-top: 85px;" />
          <p>暂无任何未解决的问题哦</p>
        </div>
      </template>
      <a-list :data-source="data" :loading="listLoading">
        <a-list-item slot="renderItem" slot-scope="item, index">
          <a-list-item-meta :description="item.author">
            <a slot="title" @click="getDetails(item.id)" target="_blank">{{
              item.title
            }}</a>
            <a slot="avatar" @click="getUser(item.userId)">
              <a-avatar
                style="cursor: pointer"
                size="small"
                :src="item.avatar ? item.avatar:''"
                :style="{
                  backgroundColor: item.avatar ? 'white' : 'green',
                  verticalAlign: 'middle',
                }"
              >
                <span style="cursor: pointer">{{ item.author }}</span>
              </a-avatar>
            </a>
          </a-list-item-meta>
          <div>{{ item.createTime }}</div>
        </a-list-item>
        <div v-if="loading && !busy" class="demo-loading-container">
          <a-spin />
        </div>
      </a-list>
    </a-config-provider>
  </div>
</template>
<script>
import infiniteScroll from "vue-infinite-scroll";
import request from "@/utils/request.js";
import { questionArticleList } from "@/apis/article";
export default {
  directives: { infiniteScroll },
  data() {
    return {
      data: [],
      loading: false,
      listLoading: false,
      busy: false,
      defaultPage: {
        page: 1,
        limit: 5,
      },
      total: 0,
    };
  },
  beforeMount() {
    this.listLoading = true;
    questionArticleList(this.defaultPage).then((res) => {
      this.data = res.data.list;
      this.total = res.data.total;
      this.listLoading = false;
    });
  },
  methods: {
    fetchData(callback) {
      questionArticleList().then((res) => {
        callback(res);
      });
    },
    getDetails(id) {
      this.$router.push("/Details/"+id);
    },
    getUser(id) {
      this.$router.push("/User/"+id);
    },
    handleInfiniteOnLoad() {
      const data = this.data;
      this.loading = true;
      if (data.length !== this.total) {
        this.defaultPage.page = this.defaultPage.page + 1;
        questionArticleList(this.defaultPage).then((res) => {
          if (data.length > 10) {
            this.$message.warning("已经到底了");
            this.busy = true;
            this.loading = false;
            return;
          }
          let list = res.data.list;
          for (let i in list) {
            this.data.push(list[i]);
          }
        });
      }
      this.loading = false;
    },
  },
};
</script>
<style>
.demo-infinite-container {
  border: 1px solid #e8e8e8;
  border-radius: 4px;
  overflow: auto;
  padding: 8px 24px;
  height: 300px;
}
.demo-loading-container {
  position: absolute;
  bottom: 40px;
  width: 100%;
  text-align: center;
}
</style>
